package com.pro.service.imple;

import com.pro.dao.OIndexMapper;
import com.pro.entity.OIndex;
import com.pro.service.OIndexService;
import com.pro.utils.DateTimeUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Logger;

/**
 * @ClassName: OIndexServiceImple
 * @Author: 笑笑
 * @DATE: 2021/12/3
 * @TIME: 下午 02:56
 */
@Service
public class OIndexServiceImple implements OIndexService {
    @Resource
    private OIndexMapper oIndexMapper;

    //根据视频字段统计其数量
    @Override
    public OIndex getCourseField(OIndex oIndex) {
        return oIndexMapper.getCourseField(oIndex);
    }

    //统计上架课程或商品
    @Override
    public OIndex getGoOnlineCount(OIndex oIndex) {
        return oIndexMapper.getGoOnlineCount(oIndex);
    }

    //根据用户创建时间统计用户数量
    @Override
    public Integer getBeforeUser(OIndex oIndex) {
        return oIndexMapper.getBeforeUser(oIndex);
    }

    //每个小时内的创建总数
    @Override
    public Map<String, Object> getCourseHours() {
        Map<String, Object> map = new HashMap<>();
        //每个小时
        String[] hoursList = new String[24];
        //每个小时内的创建总数
        int[] hoursData = new int[24];
        //根据小时获取的创建数量
        Map<String, String> courseHours = oIndexMapper.getCourseHours();
        //获取所有的key
        Set<String> integers = courseHours.keySet();
        Iterator<String> iterator = integers.iterator();
        while (iterator.hasNext()) {
            //遍历好的每一个set
            String elem = iterator.next();
            //根据key获取对应的val
            String s = String.valueOf(courseHours.get(elem));
            //截取每小时内的创建总数
            String substring = s.substring(s.indexOf('=') + 1, s.indexOf(','));
            //判断是不是0开头
            if (elem.substring(0, 1).equals(0)){
                Integer index = Integer.parseInt(elem.substring(1));
                hoursData[index] = Integer.parseInt(substring);
            }else {
                hoursData[Integer.parseInt(elem)] = Integer.parseInt(substring);
            }
        }
        for (int i = 0; i < 24; i++){
            if (i < 10){
                hoursList[i] = "0" + i + "00";
            }else {
                hoursList[i] = i + ":00";
            }
        }
        map.put("data", hoursData);
        map.put("list", hoursList);
        return map;
    }

    //每个周内的创建总数
    @Override
    public Map<String, Object> getCourseWeek() {
        Map<String, Object> map = new HashMap<>();
        //每个周
        List<String> weekList = DateTimeUtils.getWeekOfMonth(new Date());
        //每个周内内的创建总数
        int[] weekData = new int[weekList.size() - 1];
        //根据每周获取的创建数量
        Map<String, String> courseDate = oIndexMapper.getCourseDate("%Y-%m-%d");
        for (int i = 0; i < weekList.size() - 1; i++) {
            //根据key获取对应的val
            Object o = courseDate.get(weekList.get(i));
            if (o != null){
                String s = String.valueOf(o);
                //截取每周内的创建总数
                String substring = s.substring(s.indexOf('=') + 1, s.indexOf(','));
                Logger.getGlobal().info(substring);
                weekData[i] = Integer.parseInt(substring);
            }
        }
        Logger.getGlobal().info(Arrays.toString(weekData));
        map.put("data", weekData);
        map.put("list", weekList);
        return map;
    }

    //每个月中每天内的创建总数
    @Override
    public Map<String, Object> getCourseDay() {
        Map<String, Object> map = new HashMap<>();
        //月数
        int monthNumber = DateTimeUtils.getDaysOfMonth(new Date());
        //每个月
        int[] monthList = new int[monthNumber];
        //每个月内的创建总数
        int[] monthDate = new int[monthNumber];
        //根据每月获取的创建数量
        Map<String, String> courseDate = oIndexMapper.getCourseDate("%d");
        int day = 0;
        for (int i = 0; i < monthNumber; i++) {
            ++day;
            //根据key获取对应的val
            Object o = courseDate.get(day + "");
            Object z = courseDate.get("15");
            if (o != null){
                String s = String.valueOf(o);
                //截取每月内的创建总数
                String substring = s.substring(s.indexOf('=') + 1, s.indexOf(','));
                monthDate[i] = Integer.parseInt(substring);
            }
            monthList[i] = day;
        }
        map.put("data", monthDate);
        map.put("list", monthList);
        return map;
    }

    //统计每个月课程的总数
    @Override
    public Map<String, Object> getCourseMonthSum() {
        Map<String, Object> map = new HashMap<String, Object>();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        //使用Calendar获取前一个月
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, -1);

        Integer currentMonthSum = oIndexMapper.getCourseMonthSum(sdf.format(date));
        Integer LastMonthSum = oIndexMapper.getCourseMonthSum(sdf.format(cal.getTime()));
        map.put("currentMonthSum", currentMonthSum);
        map.put("lastMonthSum", LastMonthSum);
        return map;
    }

    //某个月课程申请成功的总数
    @Override
    public Map<String, Object> getCourseAdopMonthSum() {
        Map<String, Object> map = new HashMap<String, Object>();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        //使用Calendar获取前一个月
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, -1);

        Integer currentMonthSum = oIndexMapper.getCourseAdopMonthSum(sdf.format(date));
        Integer LastMonthSum = oIndexMapper.getCourseAdopMonthSum(sdf.format(cal.getTime()));
        map.put("currentMonthSum", currentMonthSum);
        map.put("lastMonthSum", LastMonthSum);
        return map;
    }
}
